package jmine.tec.services.integration;

import static jmine.tec.services.api.ActionsEnum.EXECUTAR;
import static jmine.tec.services.impl.ServicesImplMessages.REFDB_ENTRY_NOT_FOUND;

import java.io.InputStream;

import jmine.tec.di.annotation.Injected;
import jmine.tec.services.annotations.Execution;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.api.RawInput;
import jmine.tec.services.api.ServiceExecutionException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import bancosys.tec.datadigester.DataDigesterController;

/**
 * Service that loads files during an integration test
 * 
 * @author lundberg
 */
@ServiceImplementor(action = EXECUTAR)
public class CargaArquivoRefdbService {

    private static final Log LOG = LogFactory.getLog(CargaArquivoRefdbService.class);

    public static final String FILE_CODE = "Código do arquivo";

    public static final String PATH = "Caminho";

    private DataDigesterController controller;

    private String fileCode;

    /**
     * Processes a file
     * 
     * @throws Exception
     */
    @Execution
    public void execute(@Injected RawInput input) throws Exception {

        String path = input.getRaw(PATH);

        LOG.info(String.format("Efetuando carga de entrada do refdb. Caminho: %s. Código do arquivo: %s.", path, this.fileCode));
        InputStream stream = this.getClass().getResourceAsStream(path);
        if (stream == null) {
            throw new ServiceExecutionException(REFDB_ENTRY_NOT_FOUND.create(path));
        }
        this.controller.process(this.fileCode, path, stream);
    }

    /**
     * @param controller the controller to set
     */
    @Injected
    public void setController(DataDigesterController controller) {
        this.controller = controller;
    }

    /**
     * @param fileName the fileName to set
     */
    @Input(fieldName = PATH)
    public void setPath(String path) {
    }

    /**
     * @param fileCode the fileCode to set
     */
    @Input(fieldName = FILE_CODE)
    public void setFileCode(String fileCode) {
        this.fileCode = fileCode;
    }

}
